﻿Imports lbGlobalcom
Imports System.Data
Public Class fNDDsctoProveedor
    Dim daMovimi As dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
    Dim iTerce As cTercero
    Dim iTipoDoc As cTipoDocumento
    Private Sub btnBuscaDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaDoc.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.SubClase = "NDP"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDDOCUMENTOTextBox.Text = fLDoc.idTipoDoc
                CargaTipoDoc(fLDoc.idTipoDoc)
                ConfiguraByTipoDoc()
                ActualizaDatosDoc()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub ConfiguraByTipoDoc()
        Select Case iTipoDoc.DocEs
            Case 1
                btnBuscaBodOri.Enabled = False
                IDBODEGAORIGENTextBox.Enabled = False
                IDTERCEROTextBox.Enabled = False
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
            Case 2
                btnBuscaBodOri.Enabled = True
                IDBODEGAORIGENTextBox.Enabled = True
                IDTERCEROTextBox.Enabled = False
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
            Case 3
                btnBuscaBodOri.Enabled = True
                IDBODEGAORIGENTextBox.Enabled = True
                IDTERCEROTextBox.Enabled = False
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
            Case 4
                btnBuscaBodOri.Enabled = True
                IDBODEGAORIGENTextBox.Enabled = True
                IDTERCEROTextBox.Enabled = True
                SUCURSALTextBox.Enabled = True
                IDCANALTextBox.Enabled = True
                btnBuscaCanal.Enabled = True
            Case 5
                btnBuscaBodOri.Enabled = False
                IDBODEGAORIGENTextBox.Enabled = False
                IDTERCEROTextBox.Enabled = True
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
        End Select
    End Sub

    Private Sub CargaTipoDoc(ByVal TipoDoc As String)
        Try
            iTipoDoc = New cTipoDocumento(TipoDoc)
            If iTipoDoc Is Nothing Then
                MessageBox.Show("El tipo de documento seleccionado no esta registrado", "Error en tipo de documento", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(TipoDoc, iTipoDoc.DocEs)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub ActualizaDatosDoc()
        Try
            IDBODEGAORIGENTextBox.Text = iTipoDoc.BodegaDe
            IDCCOSTOTextBox.Text = iTipoDoc.idCcosto
            IDLISTATextBox.Text = iTipoDoc.idLista
            IDSEDETextBox.Text = iTipoDoc.idSede
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaSede_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaSede.Click
        Try
            Dim fLS As New fListaSedes
            If fLS.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDSEDETextBox.Text = fLS.Sede
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaBodOri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaBodOri.Click
        Try
            Dim fBusBod As New fListaBodegas
            fBusBod.idSede = IDSEDETextBox.Text
            If fBusBod.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDBODEGAORIGENTextBox.Text = fBusBod.Bodega
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaCanal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCanal.Click
        Try
            Dim fLC As New fListaCanales
            If fLC.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCANALTextBox.Text = fLC.Canal
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaCcosto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCcosto.Click
        Try
            Dim fCC As New fListaCCostos
            If fCC.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCCOSTOTextBox.Text = fCC.CCosto
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub ActualizaDatosTercero()
        Try
            iTerce = New cTercero(IDTERCEROTextBox.Text, "Pro")
            If iTerce IsNot Nothing Then
                txtNombreTercero.Text = iTerce.NombreCompleto
                TXTDIRECCION.Text = iTerce.Direccion
                Dim daMuni As New lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
                TXTCIUDAD.Text = iTerce.iMunicipios.Municipio
                If iTipoDoc.DocEs = eTipoMovimi.NDCompras Then
                    If iTerce.iProveedor Is Nothing Then
                        MessageBox.Show("Proveedor no existe verifique e intente de nuevo", "Cliente No Existe", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaTerce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTerce.Click
        Try
            Dim fBT As New fBuscaTercero
            fBT.TipoTercero = "Proveedor"
            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTERCEROTextBox.Text = fBT.midTercero
                SUCURSALTextBox.Text = fBT.mSucursal
                iTerce = New cTercero(IDTERCEROTextBox.Text, "Pro")
                ActualizaDatosTercero()
                'MOV_DETALLENOTASDEBITOBindingSource.EndEdit()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub SalvaCambios(ByVal Msj As Boolean)
        Try
            If Msj Then _
                Validate()
            MOV_MOVIMIENTOBindingSource.EndEdit()
            MOV_DETALLENOTASDEBITOBindingSource.EndEdit()
            daMovimi.Update(DsMovimiento.MOV_MOVIMIENTO)
            If Msj Then _
                MessageBox.Show("Se salvarón cambios con exito", "Salvar Cambios", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub MOV_MOVIMIENTOBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MOV_MOVIMIENTOBindingNavigatorSaveItem.Click
        Try
            SalvaCambios(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            If DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(True)
                End If
            End If
            MOV_MOVIMIENTOBindingSource.CancelEdit()
            DsMovimiento.MOV_MOVIMIENTO.Rows.Clear()
            MOV_MOVIMIENTOBindingSource.AddNew()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaLista_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaLista.Click
        Try
            Dim fLP As New fListaPrecios
            If fLP.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDLISTATextBox.Text = fLP.Lista
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fNDDsctoProveedor_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(True)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fNDDsctoProveedor_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daMovimi = New lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            DsMovimiento.MOV_MOVIMIENTO.Columns("FECHADOCUMENTO").DefaultValue = Date.Today
            DsMovimiento.MOV_MOVIMIENTO.Columns("FECHAVENCIMIENTO").DefaultValue = Date.Today
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCargarDatos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarDatos.Click
        Try
            'MOV_DETALLENOTASDEBITOBindingSource.EndEdit()
            If txtRuta.Text.Length > 0 Then
                Dim cnExcel As OleDb.OleDbConnection
                Dim rsExcel As OleDb.OleDbDataAdapter
                Dim ds As New DataSet
                'Crear una conexión a una hoja de excel
                cnExcel = New OleDb.OleDbConnection
                cnExcel.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" & txtRuta.Text & ";Extended Properties=Excel 8.0;" 'Aqui esta la clave
                rsExcel = New OleDb.OleDbDataAdapter("Select * from  [Hoja1$]", cnExcel)
                'Hacemos una consulta SQL de la hoja excel que quieras traspasar a la base de datos(en este caso, se llama Hoja1)
                cnExcel.Open()
                'Rellenar el recordset de Access
                rsExcel.Fill(ds, "tabla1")
                For Each dr As DataRow In ds.Tables(0).Rows

                Next
            Else
                MessageBox.Show("Debe seleccionar un Archivo", "Archivo", MessageBoxButtons.OK)
            End If
            'Y ahora traspasamos los datos del excel a la tabla access
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaArchivo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaArchivo.Click
        Try
            If opdRuta.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtRuta.Text = opdRuta.FileName
            End If
        Catch ex As Exception

        End Try
    End Sub

   
End Class